package com.ruoyi.supplier.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.supplier.domain.vo.StockVo;
import com.ruoyi.supplier.domain.bo.StockBo;
import com.ruoyi.supplier.service.IStockService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 仓库控制器
 * 前端访问路由地址为:/supplier/stock
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/stock")
public class StockController extends BaseController {

    private final IStockService iStockService;

    /**
     * 查询仓库列表
     */
    @SaCheckPermission("supplier:stock:list")
    @GetMapping("/list")
    public TableDataInfo<StockVo> list(StockBo bo, PageQuery pageQuery) {
        return iStockService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出仓库列表
     */
    @SaCheckPermission("supplier:stock:export")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StockBo bo, HttpServletResponse response) {
        List<StockVo> list = iStockService.queryList(bo);
        ExcelUtil.exportExcel(list, "仓库", StockVo.class, response);
    }

    /**
     * 获取仓库详细信息
     *
     * @param stockId 主键
     */
    @SaCheckPermission("supplier:stock:query")
    @GetMapping("/{stockId}")
    public R<StockVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer stockId) {
        return R.ok(iStockService.queryById(stockId));
    }

    /**
     * 新增仓库
     */
    @SaCheckPermission("supplier:stock:add")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StockBo bo) {
        return toAjax(iStockService.insertByBo(bo));
    }

    /**
     * 修改仓库
     */
    @SaCheckPermission("supplier:stock:edit")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StockBo bo) {
        return toAjax(iStockService.updateByBo(bo));
    }

    /**
     * 删除仓库
     *
     * @param stockIds 主键串
     */
    @SaCheckPermission("supplier:stock:remove")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stockIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Integer[] stockIds) {
        return toAjax(iStockService.deleteWithValidByIds(Arrays.asList(stockIds), true));
    }
}
